/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.threadtask.workthread;

import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.DateUtils;
import com.xlx.openLink.entity.pojo.Port;
import com.xlx.openLink.entity.pojo.Speed;
import com.xlx.openLink.entity.snmpenity.MibIfEntry;
import com.xlx.openLink.service.AlarmService;
import com.xlx.openLink.service.DeviceService;
import com.xlx.openLink.service.SpeedService;
import com.xlx.openLink.uitls.Commuitls;
import com.xlx.openLink.vo.DevicePortVO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 计算网速
 *
 * @author Admin
 */
@Slf4j
public class SearchSpeedThread implements Runnable {

    private List<DevicePortVO> devicePortVOList;

    private DeviceService deviceService;

    private SpeedService speedService;

    private AlarmService alarmService;

    private Integer DELAY_TIME = 5 * 1000;

    public SearchSpeedThread(List<DevicePortVO> devicePortVOList, DeviceService deviceService, SpeedService speedService, AlarmService alarmService) {
        this.devicePortVOList = devicePortVOList;
        this.deviceService = deviceService;
        this.speedService = speedService;
        this.alarmService = alarmService;
    }

    @Override
    public void run() {
        List<Speed> curList = new ArrayList<>();
        List<Speed> perList = new ArrayList<>();
        List<Speed> ls = new ArrayList<>();
        try {
            doBiz(devicePortVOList, perList);
            Thread.sleep(2 * 1000);
            doBiz(devicePortVOList, curList);
            for (Speed cur : curList) {
                for (Speed per : perList) {
                    if (cur.equals(per) &&
                            (cur.getSpOutoctets() != 0 && per.getSpOutoctets() != 0 && cur.getSpInoctets() != 0 && per.getSpInoctets() != 0)) {
                        cur.setSpOutSpeed(Commuitls.doubleCalc(cur.getSpOutoctets(), per.getSpOutoctets(), DateUtils.dateDiffByMsec(per.getSpCreateDate(), cur.getSpCreateDate())));
                        cur.setSpInSpeed(Commuitls.doubleCalc(cur.getSpInoctets(), per.getSpInoctets(), DateUtils.dateDiffByMsec(per.getSpCreateDate(), cur.getSpCreateDate())));
                        ls.add(cur);
                        log.info("端口号:{}",cur.getSpPIndex() + "");
                        log.info("入网网速" + cur.getSpInSpeed().toString());
                        log.info("出网网速" + cur.getSpOutSpeed().toString());
                        break;
                    }
                }
            }
            speedService.insertBatch(ls);
            //产生告警
            alarmService.dealAlarm(ls);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("--------------------线程结束-------------------------");

    }

    /**
     * 获取端口信息
     *
     * @param devicePortVOList
     * @param list
     */
    private void doBiz(List<DevicePortVO> devicePortVOList, List<Speed> list) {
        devicePortVOList.forEach(devicePortVO -> {
            List<Port> ports = devicePortVO.getPortList() == null ? new ArrayList<>() : devicePortVO.getPortList();
            List<MibIfEntry> mibIfEntries = deviceService.getMibIfEntry(
                    devicePortVO.getDevIp(),
                    devicePortVO.getDevSnmpPort(),
                    devicePortVO.getDevSnmpVersion(),
                    devicePortVO.getDevReadcommunity());
            for (MibIfEntry mibIfEntry : mibIfEntries) {
                /**
                 * mibIfEntry.getIfType() == 6 ||
                 *                         mibIfEntry.getIfType() == 117 ||
                 *                         mibIfEntry.getIfType() == 64 ||
                 */
                // if (mibIfEntry.getIfType() == 71 && mibIfEntry.getIfOperStatus() == 1) {
                for (Port port : ports) {
                    //排除网速为空状态的网速
                    if (port.getpIndex() == mibIfEntry.getIfIndex() && mibIfEntry.getIfOutOctets() > 0 && mibIfEntry.getIfInOctets() > 0) {
                        Speed speed = new Speed();
                        speed.setPkGuid(BaseUtils.createGUID());
                        speed.setDevId(devicePortVO.getPkGuid());
                        speed.setpId(port.getPkId());
                        speed.setSpDevName(devicePortVO.getDevName());
                        speed.setSpPName(port.getpName());
                        speed.setSpCreateDate(new Date());
                        speed.setSpPIndex(port.getpIndex());
                        speed.setSpInoctets(mibIfEntry.getIfInOctets());//进
                        speed.setSpOutoctets(mibIfEntry.getIfOutOctets());//出
                        list.add(speed);
                        break;
                    }
                }
                //}
            }
        });

    }

    /**
     * 延迟方法
     *
     * @throws InterruptedException
     */
    private void extracted() throws InterruptedException {
        //5秒延迟
        Thread.sleep(DELAY_TIME);
    }

}
